home *** CD-ROM | disk | FTP | other *** search
/ New Masters of Flash / New Masters of Flash.iso / pc / MOVIES / 05.dir / 00208_Text_text10.txt < prev    next >
Text File  |  2000-10-01  |  3KB  |  33 lines

  1. Frame 1: 
  2. (label Sine & Cosine) Sine & Cosine Functions in Flash
  3.  
  4. Given a particular angle, Sine and Cosine gives you the coordinate at which the angle intersects a circle. The points in the 3D model travel on a circular path around the center point (0,0,0), so Sine and Cosine are used to calculate the position of each point as the angle of rotation around the center point for each axis is increased or decreased. On a two- dimensional plane, Cosine represents the Horizontal (X) value and Sine is the vertical (Y) value for the coordinate.
  5. The Sine and Cosine values for an angle are based on a circle with a radius of one unit. The formula for working out the X coordinate on a circle is: 
  6.     
  7.     radius * cos (angle) 
  8. and for the Y coordinate is 
  9.     radius * sin (angle). 
  10.  
  11. If youΓÇÖre using Flash4, which doesnΓÇÖt have these functions built into its scripting language, the first step would be to find a way to do these trigonometry functions and that is what this script in this frame deals with specifically You could do these calculations in real time using hideously long expressions, but this would slow things down considerably. Even if these functions were built into Flash4 (as they are in Director) it would still be quite CPU-intensive to do these calculations in real time. The alternative to this would be to create a lookup list or often referred to as the lookup method.
  12. You can do this by setting the values for an array of variables to the values of the corresponding Sine or Cosine values. You could then extract this value whenever you needed it, without doing the calculations. You could also have used Math.Sin(x) to calculate this, remembering that the Flash 5 Math.Trig functions use radians, but we have left the original Flash 4 lookup intact. To create this lookup list, I used the following script to set an array of variables:
  13.  
  14. sin0 = .0;
  15. sin1 = .01745;
  16. sin2 = .0349;
  17. sin3 = .05234;
  18. …
  19.  
  20. sin89 = .9998;
  21. sin90 = 1;
  22. This sets the approximate Sine values for angles 0 to 90. From these values you can derive the values for angles 91 to 360. You can see in the graph below that the Sine and Cosine values for angles 0 to 360 range from 1 to -1 and that the Cosine value of an angle equals the Sine value of the same angle plus 90 degrees. The Sine and Cosine values are thus offset by 90 degrees.
  23.  
  24. Because Flash 5 now includes math functions, we will only briefly touch on how the full 360 lookup table was produced. You can set the Sine values for the next 90 degrees by counting backwards, setting the values of variables: sin90 through to sin180 to the values of variables sin90 to sin0.
  25. To arrive at Sine values for the remaining 180 degrees, you simply need to set the values of variables sin180 through sin360 to the minus values of variables sin0 through sin180.
  26.  
  27. To set the Cosine values I use the following script that sets the Cosine value for an angle to the Sine value of the angle, plus 90 degrees. So the values of variables cos0 to cos270 are set to the values of variables sin90 to sin360.
  28.  
  29. And the values of variables cos270 to cos360 are set to the values of variables sin0 to sin90.
  30.  
  31. When you run the script, two sets of arrayed variables will be set in the movie: variables: sin0 to sin360 and variables cos0 to cos360. To extract the Sine value of a particular angle (X) simply use eval("sin" add X) and eval("cos" add X) for Cosine.
  32. Frame 2: (label Vertex Coordinates)
  33.